package com.ppc.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ppc.pmatch.Ciphertext;
import com.ppc.pmatch.RequireTag;
import com.ppc.pmatch.SKObj;
import com.ppc.pmatch.Trapdoor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @author dy
 * @date 2023/12/20
 * @descrpition 连接匹配数据库的工具类
 */

public class MatchServerUtil {

    public static final String ip = "http://localhost:8090";

  //public static final String ip = "http://47.120.48.60:8090";


  /**
   * @param SK   从数据库读取该工人的私钥
   * @param tags 标签
   * @return {@link List }<{@link Long }>
   * @description 工人发起match请求
   */
  public static List<Long> PostMatch(SKObj SK, String[] tags) {
    List<Trapdoor> trapList = PMatchUtil.trapWorker(SK, tags);
    JSONArray jsonArray = new JSONArray(trapList);
    List<Long> result = new ArrayList<Long>();
    System.out.println(jsonArray);
    for (int i = 0; i < jsonArray.size(); i++) {
      JSONObject json = jsonArray.getJSONObject(i);
      System.out.println(json);
      String HTTPResult = HttpClientUtil.DO_POST_string(ip + "/crowd/match", json); // 第一次接收json
      System.out.println(HTTPResult); // {"code":1,"msg":null,"data":[1,3]}
      JSONObject resultJSON = JSONObject.parseObject(HTTPResult);
      JSONArray resultArray = (JSONArray) resultJSON.get("data");
      List<Long> list = resultArray.toJavaList(Long.class);
      result.addAll(list);
    } //请求端处理去重问题
    Collections.sort(result); // 排序即可
    return result;
  }

  /**
   * 雇主上传需求
   *
   * @param requireId
   * @param tag
   * @return {@link JSONObject }
   * @description
   */
  public static JSONObject addRequire(long requireId, String tag) {
    Ciphertext C = PMatchUtil.indexGen(tag);
    RequireTag requireTag = new RequireTag(requireId, C);
    // post上传
    //变成json对象
    JSONObject json = new JSONObject();
    json.put("requireId", requireTag.getRequireId());
    json.put("c1", requireTag.getC1());
    json.put("c2", requireTag.getC2());
    json.put("c3", requireTag.getC3());
    json.put("c4", requireTag.getC4());
    //System.out.println(json);
    JSONObject result = HttpClientUtil.DO_POST(ip + "/crowd/add", json);
    return result;
  }

  /**
   * 雇主修改需求标签
   *
   * @param requireId
   * @param tag
   * @return {@link JSONObject }
   * @description
   */
  public static JSONObject changeRequire(long requireId, String tag) {
    Ciphertext C = PMatchUtil.indexGen(tag);
    RequireTag requireTag = new RequireTag(requireId, C);
    // post上传
    //变成json对象
    JSONObject json = new JSONObject();
    json.put("requireId", requireTag.getRequireId());
    json.put("c1", requireTag.getC1());
    json.put("c2", requireTag.getC2());
    json.put("c3", requireTag.getC3());
    json.put("c4", requireTag.getC4());
    System.out.println(json);
    JSONObject result = HttpClientUtil.DO_POST(ip + "/crowd/change", json);
    return result;
  }

  /**
   * 雇主删除需求
   *
   * @param requireId
   * @return {@link String }
   * @description
   */
  public static String deleteRequire(long requireId) {
    String result = HttpClientUtil.doGet(ip + "/crowd/delete/" + requireId, null);
    return result;
  }
}
